import Vue from "vue";
import VueRouter from "vue-router";
import { getStoageIsLogin } from "@/config/Utils";

Vue.use(VueRouter);

const userList = (r) => require.ensure([], () => r(require("@/page/userList.vue")), "userList");
const login = (r) => require.ensure([], () => r(require("@/page/login.vue")), "login");
const home = (r) => require.ensure([], () => r(require("@/page/home.vue")), "home");
const addUser = (r) => require.ensure([], () => r(require("@/page/addUser.vue")), "addUser");
const userImage = (r) => require.ensure([], () => r(require("@/page/userImage.vue")), "userImage");
const businessList = (r) => require.ensure([], () => r(require("@/page/businessList.vue")), "businessList");
const addBusiness = (r) => require.ensure([], () => r(require("@/page/addBusiness.vue")), "addBusiness");
const addFoodstuff = (r) => require.ensure([], () => r(require("@/page/addFoodstuff.vue")), "addFoodstuff");
const foodstuffList = (r) => require.ensure([], () => r(require("@/page/foodstuffList.vue")), "foodstuffList");
const ordersList = (r) => require.ensure([], () => r(require("@/page/ordersList.vue")), "ordersList");
const textUpdate = (r) => require.ensure([], () => r(require("@/page/textUpdate.vue")), "textUpdate");
const userSet = (r) => require.ensure([], () => r(require("@/page/userSet.vue")), "userSet");
const saySum = (r) => require.ensure([], () => r(require("@/page/saySum.vue")), "saySum");
const index = (r) => require.ensure([], () => r(require("@/page/index.vue")), "index");

const routes = [
  {
    path: "/",
    component: login,
  },
  {
    path: "/admin",
    component: home,
    children: [
      {
        path: "addUser",
        component: addUser,
        meta: ["添加数据", "添加用户"],
      },
      {
        path: "userList",
        component: userList,
        meta: ["数据管理", "用户列表"],
      },
      {
        path: "userImage",
        component: userImage,
        meta: ["图表", "用户分布"],
      },
      {
        path: "businessList",
        component: businessList,
        meta: ["数据管理", "商家列表"],
      },
      {
        path: "addBusiness",
        component: addBusiness,
        meta: ["添加数据", "添加商铺"],
      },
      {
        path: "addFoodstuff",
        name: "addFoodstuff",
        component: addFoodstuff,
        meta: ["添加数据", "添加食品"],
        props: true,
      },
      {
        path: "foodstuffList",
        component: foodstuffList,
        meta: ["数据管理", "食品列表"],
      },
      {
        path: "ordersList",
        component: ordersList,
        meta: ["数据管理", "订单列表"],
      },
      {
        path: "textUpdate",
        component: textUpdate,
        meta: ["编辑", "文本编辑"],
      },
      {
        path: "userSet",
        component: userSet,
        meta: ["设置", "管理员设置"],
      },
      {
        path: "saySum",
        component: saySum,
        meta: ["说明", "网站说明"],
      },
      {
        path: "index",
        component: index,
        meta: ["数据查看", "折线图表"],
      },
    ],
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  const flag = getStoageIsLogin();
  if (to.path !== "/" && !flag) {
    next("/");
  }
  next();
});
export default router;
